<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;  
use Illuminate\Support\Facades\DB;
use App\ShopComments;
use App\Product_comment;
use App\Shop;
use App\Product_cat;

Class CommentsController extends AdminController{
    
    public function info(Request $request){
        $shopId =  trim($request->input('shop_id', ''));
        $shop = ShopComments::where('shop_id', $shopId)->first();
        $shop = $shop->toArray();
        $shopModel = Shop::where('member_id', $shopId)->first();
        $shop['logo'] =$shopModel->logo;
        $this->output($shop);
    }
    public function shoplist(Request $request){
        $fliter = ShopComments::where('id','>', 0);
        $shop_name = trim($request->input('shop_name', ''));
        if($shop_name){
            $fliter = $fliter->where('company','like', '%'. $shop_name . '%');
        }
        $orderColumn = trim($request->input('order_column', 'total_comments'));
        $orderType = trim($request->input('order_type', 'desc'));
        $this->itemsWithPage($request, $fliter, array(),$orderColumn, $orderType);
    }
    
    public function commentslist(Request $request){
        $shopId = trim($request->input('shop_id', ''));
        $shop = ShopComments::where('shop_id', $shopId)->first();
        $fliter = Product_comment::where('shop_id', $shopId);
        $search = array();
        $search['star']=trim($request->input('star', ''));
        $search['is_default']=trim($request->input('is_default', ''));//'1主动，2默认好评',
        $search['hasimg']=trim($request->input('hasimg', ''));//1带图，2不带图
        
        $search['is_gold']=trim($request->input('is_gold', ''));//2加精
        $search['is_show']=trim($request->input('is_show', ''));//2隐藏
        $search['product_cat_id']=trim($request->input('product_cat_id', ''));//2隐藏
        
        $name=trim($request->input('name', ''));//商品名称
        if($name){
            $fliter = $fliter->where('name','like', '%'. $name . '%');
        }
        $this->itemsWithPage($request, $fliter, $search);
    }
    
    /**
     * 显示还是隐藏
     * @param Request $request
     */
    public function showHide(Request $request){
        $id = trim($request->input('id', ''));
        $is_show = trim($request->input('is_show', ''));
        Product_comment::where('id', $id)->update([
            'is_show'=>$is_show
        ]);
        $this->output(array());
    }
    
    /**
     * 显示还是隐藏
     * @param Request $request
     */
    public function gold(Request $request){
        $id = trim($request->input('id', ''));
        $is_show = trim($request->input('is_gold', ''));
        Product_comment::where('id', $id)->update([
            'is_gold'=>$is_show
        ]);
        $this->output(array());
    }
    
    /**
     * 显示还是隐藏
     * @param Request $request
     */
    public function top(Request $request){
        $id = trim($request->input('id', ''));
        $is_show = trim($request->input('is_top', ''));
        Product_comment::where('id', $id)->update([
            'is_top'=>$is_show
        ]);
        $this->output(array());
    }
    
    
    public function shopCat(Request $request){
        $shopId = (int) trim($request->input('shop_id', ''));
        $sql = "select distinct catid as catid from mega_product where member_id =$shopId";
        $result = DB::select($sql);
        $ids = array();
        foreach ($result as $line){
            $ids[] = $line->catid;
        }
        $items = Product_cat::whereIn('id', $ids)->select('id', 'cat_name')->get();
        $this->output($items);
    }
}